package com.xps.and.yuntong.Utils;

import android.os.Environment;
import android.util.Log;

import com.xps.and.yuntong.App;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.jude.utils.JUtils;

//讯飞语音工具类
public class TtsUtil implements InitListener {
    private static final TtsUtil ourInstance = new TtsUtil();
    private static String TAG = "TtsUtil";
    private static String words = "";

    private SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(App.getInstance(), this);

    public static TtsUtil getInstance() {
        return ourInstance;
    }

    private TtsUtil() {
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
        mTts.setParameter(SpeechConstant.SPEED, "50");
        mTts.setParameter(SpeechConstant.PITCH, "50");
        mTts.setParameter(SpeechConstant.VOLUME, "50");
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/msc/tts.wav");
    }

    public void startSpeaking(String w) {
        boolean enableSpeak = JUtils.getSharedPreference().getBoolean(SPConstants.VOICE_ENABLE, true);
        Log.d(TAG, "enableSpeak" + enableSpeak);
        if (!enableSpeak) return;
        words = w;
        if (mTts != null) mTts.startSpeaking(w, null);
    }

    @Override
    final public void onInit(int code) {
        if (code != ErrorCode.SUCCESS) {
            Log.d(TAG, "失败 InitListener init() code = " + code);
        } else {
            mTts.startSpeaking(words, null);
        }
    }
}
